package iscript.uiFramework.math 
{
	public class MathUtil 
	{
		import flash.geom.Point;
		
		public function MathUtil() 
		{
		}		
		
		/**
		 * 根据角度获得弧度
		 * @param	Number angle
		 */
		static public function  getRadian(angle:Number):Number  {
			return  angle * Math.PI / 180;
			}
		
		/**
		 * 根据弧度获得角度
		 * @param	Number radian
		 */
		static public function  getAngle(radian:Number):Number  {
			return  radian*180/Math.PI;
			}
			
		 static public function  getRawAngle(radian:Number):Number  {
			  var value:Number = radian * 180 / Math.PI;
			  if(value<0) value+=360
			 return  value;
			}
			
		/**
		 * 获得两点之间的距离
		 * @param	p1
		 * @param	p2
		 * @return
		 */
		static public function  getPointDistance(p1:Point,p2:Point):Number {
			var dx:Number = p2.x - p1.x; 	
			var dy:Number = p2.y - p1.y;	
			return  Math.sqrt(dx * dx + dy * dy);
		}
		
				
		/**
		 * 求m和n的最大公约数
		 * 公约数是指两个数或多个数都能同时被这个数整除,这个数就叫作公约数.如4和8都能整除2,2就是它们的公约数.  又如3和6和9都能整除3,3就是它们的公约数. 
		 * @param m
		 * @param n
		 * @return
		 */
		static public function gcd(m:int, n:int):int {
			var tmp:int;
			if(m == 0){
				return n;
			}
			if(n == 0){
				return m;
			}

			if(m < n){
				tmp= m;
				m = n;
				n = tmp;
			}

			while(n != 0){
				tmp= m % n;
				m = n;
				n = tmp;
			}
			return m;
		}
		
		
		/**
		 * 一个点是是否在一堆点组成的多边形的里面
		 * @param	pointList
		 * @param	p
		 * @return
		 */
		static public function insidePolygon(pointList:Array, p:Point):Boolean
		{
			var counter:int = 0;
			var i:int;
			var xinters:Number;
			var p1:Point;
			var p2:Point;
			var n:int = pointList.length;
 
			p1 = pointList[0] as Point;
			for (i = 1; i <= n; i++)
			{
				p2 = pointList[i % n] as Point;
				if (p.y > Math.min(p1.y, p2.y))
				{
					if (p.y <= Math.max(p1.y, p2.y))
					{
						if (p.x <= Math.max(p1.x, p2.x))
						{
							if (p1.y != p2.y) {
								xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
								if (p1.x == p2.x || p.x <= xinters)
									counter++;
							}
						}
					}
				}
				p1 = p2;
			}
			if (counter % 2 == 0)
			{
				return(false);
			}
			else
			{
				return(true);
			}
		}
		
		
	}

}